package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysRoleService;
import com.atguigu.spzx.model.pojo.system.SysRole;
import com.atguigu.spzx.model.request.system.QueryRequest;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.utils.DYLAssert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author dyl
 * @version 1.0
 * @description:
 * @date 2024/12/29 18:20
 */
@RestController
@RequestMapping("/user/system/sysRole")
@Tag(name = "角色接口")
@CrossOrigin(allowCredentials = "true", originPatterns = "*", allowedHeaders = "*")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @Operation(summary = "分页查询")
    @GetMapping("/findRoleByPage/{pageNum}/{pageSize}")
    public Result<Page<SysRole>> findPage(
            @PathVariable(value = "pageNum", required = true) Integer pageNum,
            @PathVariable(value = "pageSize", required = true) Integer pageSize,
            QueryRequest queryRequest) {
        DYLAssert.notNull(pageNum, "分页错误");
        DYLAssert.notNull(pageSize, "分页错误");
        DYLAssert.notNull(queryRequest, "分页错误");
        Page<SysRole> pages = sysRoleService.findPage(pageNum, pageSize, queryRequest);
        return Result.ok(pages);
    }

    @Operation(summary = "添加角色")
    @PostMapping("/addRole")
    public Result addRole(@RequestBody SysRole sysRole) {
        DYLAssert.isTrue(StringUtils.isNotEmpty(sysRole.getRoleName()), "角色名不能为空");
        DYLAssert.isTrue(StringUtils.isNotEmpty(sysRole.getRoleCode()), "角色编码不能为空");
        boolean save = sysRoleService.save(sysRole);
        DYLAssert.isTrue(save, "添加失败");
        return Result.ok();
    }

    @Operation(summary = "根据id查询角色")
    @GetMapping("/findById/{id}")
    public Result<SysRole> findById(@PathVariable("id") Long id) {
        DYLAssert.notNull(id, "查询id不能为空");
        return Result.ok(sysRoleService.getById(id));
    }

    @Operation(summary = "修改角色")
    @PutMapping("/updateById")
    public Result updateById(@RequestBody SysRole sysRole) {
        if (StringUtils.isAnyEmpty(sysRole.getRoleName(), sysRole.getRoleCode())) {
            return Result.failed("修改失败");
        }
        DYLAssert.isTrue(sysRoleService.updateById(sysRole), "修改失败");
        return Result.ok();
    }

    @Operation(summary = "删除角色")
    @DeleteMapping("/deleteById/{id}")
    public Result updateById(@PathVariable(value = "id", required = true) Long id) {
        DYLAssert.notNull(id, "删除失败");
        DYLAssert.isTrue(sysRoleService.removeById(id), "删除失败");
        return Result.ok();
    }

    @Operation(summary = "批量删除")
    @DeleteMapping("/batchDelete")
    public Result updateById(@RequestBody List<Long> idList) {
        DYLAssert.notNull(idList, "删除失败");
        DYLAssert.isTrue(sysRoleService.removeBatchByIds(idList), "删除失败");
        return Result.ok();
    }

    @Operation(summary = "查询所有角色")
    @GetMapping("/findRoles")
    public Result<List<SysRole>> findRoles() {
        List<SysRole> roleList = sysRoleService.list();
//        List<String> nameList = roleList.stream().map(
//                SysRole::getRoleName
//        ).collect(Collectors.toList());
        return Result.ok(roleList);

    }
}
